// src/router/index.ts
import { createRouter, createWebHistory } from 'vue-router'
import BasicLayout from '@/layouts/BasicLayout.vue'
import LoginLayout from '@/layouts/LoginLayout.vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: BasicLayout,
      children: [
        { path: '', component: () => import('@/pages/HomePage.vue') },
        { path: '/picture/create', component: () => import('@/pages/picture/AddPicturePage.vue') },
        {
          path: '/picture/manager', 
          component: () => import('@/pages/picture/pictureManagePage.vue'),
          beforeEnter: (to, from, next) => {
            // 检查用户权限
            const loginUserStore = useLoginUserStore();
            if (loginUserStore.loginUser.userRole === 'admin') {
              next();
            } else {
              // 普通用户访问管理员页面时重定向到首页
              next('/');
            }
          }
        },
        { 
          path: '/history', 
          component: () => import('@/pages/user/BrowsingHistoryPage.vue'),
          beforeEnter: (to, from, next) => {
            // 检查用户是否已登录
            const loginUserStore = useLoginUserStore();
            if (loginUserStore.loginUser.userName !== '未登录') {
              next();
            } else {
              // 未登录用户重定向到登录页
              next('/user/login');
            }
          }
        },
        { 
          path: '/review', 
          component: () => import('@/pages/user/PendingReviewPage.vue'),
          beforeEnter: (to, from, next) => {
            // 检查用户是否已登录
            const loginUserStore = useLoginUserStore();
            if (loginUserStore.loginUser.userName !== '未登录') {
              next();
            } else {
              // 未登录用户重定向到登录页
              next('/user/login');
            }
          }
        },
        {
          path: '/admin/userManage',
          component: () => import('@/pages/admin/UserManagePage.vue'),
          beforeEnter: (to, from, next) => {
            // 检查用户权限
            const loginUserStore = useLoginUserStore();
            if (loginUserStore.loginUser.userRole === 'admin') {
              next();
            } else {
              // 普通用户访问管理员页面时重定向到首页
              next('/');
            }
          }
        },
        {
          path: '/admin/categoryManage',
          component: () => import('@/pages/admin/categoryManagePage.vue'),
          beforeEnter: (to, from, next) => {
            // 检查用户权限
            const loginUserStore = useLoginUserStore();
            if (loginUserStore.loginUser.userRole === 'admin') {
              next();
            } else {
              // 普通用户访问管理员页面时重定向到首页
              next('/');
            }
          }
        },
        {
          path: '/admin/tagManage',
          component: () => import('@/pages/admin/tagManagePage.vue'),
          beforeEnter: (to, from, next) => {
            // 检查用户权限
            const loginUserStore = useLoginUserStore();
            if (loginUserStore.loginUser.userRole === 'admin') {
              next();
            } else {
              // 普通用户访问管理员页面时重定向到首页
              next('/');
            }
          }
        },
        { 
          path: '/picture/addBatch', 
          component: () => import('@/pages/picture/AddPictureBatchPage.vue'),
          beforeEnter: (to, from, next) => {
            // 检查用户权限
            const loginUserStore = useLoginUserStore();
            if (loginUserStore.loginUser.userRole === 'admin') {
              next();
            } else {
              // 普通用户访问管理员页面时重定向到首页
              next('/');
            }
          }
        }
      ]
    },
    {
      path: '/user',
      component: LoginLayout,
      children: [
        { path: 'login', component: () => import('@/pages/user/UserLoginPage.vue') },
        { path: 'register', component: () => import('@/pages/user/UserRegisterPage.vue') },
        { path: 'center', component: () => import('@/pages/user/UserCenterPage.vue') }, // 用户中心
        { path: 'profile', component: () => import('@/pages/user/UserProfilePage.vue') }// 个人资料
      ]
    }
  ],
})

export default router